using System.Collections;
using UnityEngine;

public class AreaMarker : MonoBehaviour
{
	private struct State
	{
		public AreaMarkerStateType type;

		public float ellapsedTime;

		public float actionTime;
	}

	public FireShotUI fireUI;

	public UISprite marker;

	public UISprite bonusSprite;

	private Transform transform_;

	protected PowerUpType powerUpOnDisplay;

	private State state;

	public FloatRange timeRange = new FloatRange
	{
		min = 4f,
		max = 20f
	};

	private bool initialized_;

	public ParticleSystem fireParticles;

	public Color normalColor;

	public Color blinkColor;

	public float blinkDuration = 0.1f;

	public int blinkTimes = 3;

	protected PlayerBase owningPlayer;

	public float bonusDuration = 0.2f;

	public float bonusFromAlpha = 0.2f;

	public float bonusToAlpha = 1f;

	public Vector3 bonusMoveFrom = Vector3.zero;

	public Vector3 bonusMoveTo = Vector3.forward * -10f;

	public Transform cachedTransform
	{
		get
		{
			return marker.cachedTransform;
		}
	}

	public float width { get; protected set; }

	public float length { get; protected set; }

	private void Init()
	{
		if (!initialized_)
		{
			Hide();
			initialized_ = true;
		}
	}

	public void SetOwningPlayer(PlayerBase player)
	{
		owningPlayer = player;
	}

	public void Awake()
	{
		Init();
		base.transform.localScale = Vector3.one / 64f;
		base.transform.localRotation = Quaternion.AngleAxis(90f, Vector3.right);
		SetState(AreaMarkerStateType.NotDisplaying);
	}

	protected void Show(Vector3 center, float width, float length)
	{
		Init();
		SetState(AreaMarkerStateType.Displaying);
		marker.cachedGameObject.SetActive(true);
		base.transform.position = center;
		Vector3 localScale = cachedTransform.localScale;
		this.width = width;
		this.length = length;
		localScale.x = width;
		localScale.z = length;
		cachedTransform.localScale = localScale;
		bonusSprite.alpha = 0f;
		marker.color = normalColor;
	}

	private void SetState(AreaMarkerStateType type)
	{
		state = default(State);
		state.type = type;
		if (state.type == AreaMarkerStateType.NotDisplaying)
		{
			state.actionTime = Random.Range(timeRange.min, timeRange.max);
		}
	}

	public void Hide()
	{
		marker.cachedGameObject.SetActive(false);
	}

	public bool isPositionInAreaMarker(Vector3 position)
	{
		Vector3 position2 = cachedTransform.position;
		return Mathf.Abs(position.x - position2.x) <= width * 0.5f && Mathf.Abs(position.z - position2.z) <= length * 0.5f;
	}

	private void ShowOnRandomPos()
	{
		float num = Random.Range(0.2f, 0.5f) * Table.halfwidth;
		float num2 = Random.Range(1f, 1f) * Table.halfLength;
		float num3 = Table.halfwidth - num * 0.5f;
		float num4 = Table.halfLength * 0.5f - num2 * 0.5f;
		Vector3 center = new Vector3(Random.Range(0f - num3, num3), Table.tabletopy, (float)(-owningPlayer.tableSide) * (Random.Range(0f - num4, num4) + Table.halfLength * 0.5f));
		Show(center, num, num2);
	}

	private void Update()
	{
		if (!(owningPlayer == null) && owningPlayer.match.ball.isBallInGame)
		{
			state.ellapsedTime += Time.deltaTime;
		}
	}

	public void BallHit(Ball ball)
	{
		if (state.type == AreaMarkerStateType.Displaying)
		{
			bool flag = isPositionInAreaMarker(ball.flightPlan.landingPos);
			StopAllCoroutines();
			if (flag)
			{
				StartCoroutine(DoBallHitMarker(ball.flightPlan.timeToLand, ball.flightPlan.playerThatHitBall));
			}
		}
		HumanPlayer humanPlayer = ball.flightPlan.playerThatHitBall as HumanPlayer;
		if (humanPlayer != null)
		{
			fireUI.SetFireAmount((float)humanPlayer.markerHits / (float)humanPlayer.match.ui.racketButton.currentRacket.areaMarkerHits);
		}
	}

	public void OpponentHitBall(Ball ball)
	{
		if (state.type == AreaMarkerStateType.NotDisplaying && !(state.ellapsedTime > state.actionTime))
		{
		}
	}

	public void SetBlinkColor()
	{
		marker.color = blinkColor;
	}

	private IEnumerator DoBlink(float duration, int blinkTimes)
	{
		for (int i = 0; i < blinkTimes; i++)
		{
			yield return new WaitForSeconds(duration);
			marker.color = blinkColor;
			yield return new WaitForSeconds(duration);
			marker.color = normalColor;
		}
	}

	private IEnumerator DoBallHitMarker(float durationTillHit, PlayerBase player)
	{
		yield return new WaitForSeconds(durationTillHit);
		SetState(AreaMarkerStateType.Transitioning);
		HumanPlayer humanPlayer = player as HumanPlayer;
		if (humanPlayer != null)
		{
			humanPlayer.markerHits++;
			fireUI.SetFireAmount((float)humanPlayer.markerHits / (float)humanPlayer.match.ui.racketButton.currentRacket.areaMarkerHits);
			StartCoroutine(DoShowBonus());
		}
	}

	private IEnumerator DoShowBonus()
	{
		float duration = bonusDuration;
		float time = 0f;
		SetBlinkColor();
		while (time < duration)
		{
			time += Time.deltaTime;
			bonusSprite.alpha = Mathf.Lerp(bonusFromAlpha, bonusToAlpha, time / duration);
			bonusSprite.cachedTransform.localPosition = Vector3.Lerp(bonusMoveFrom, bonusMoveTo, time / duration);
			yield return null;
		}
		bonusSprite.alpha = 0f;
		bonusSprite.cachedTransform.localPosition = bonusMoveFrom;
		Hide();
		SetState(AreaMarkerStateType.NotDisplaying);
	}
}
